package tree.tree2.symmetry.BinarySearchTree.ModifyAndConstruct;

import tree.TreeNode;

/**
 * @ClassName ConstructBSTBySortedArray
 * @Description TODO
 * @Author lenovo
 * @Date 2023-05-11 11:21
 * @Version 1.0
 * @Comment Magic. Do not touch.
 * If this comment is removed. the program will blow up
 */
public class ConstructBSTBySortedArray {

    public TreeNode sortedArrayToBST(int[] nums) {

        return constructBST(nums, 0, nums.length);
    }


    // 0,1,2
    private TreeNode constructBST(int[] nums, int left, int right) {
        if(left >= right) return null;
        if(right - left == 1) return new TreeNode(nums[left]);

        int mid = left + (right-left)/2;

        TreeNode root = new TreeNode(nums[mid]);

        root.left = constructBST(nums,left,mid);
        root.right = constructBST(nums,mid+1,right);
        return root;
    }

    public static void main(String[] args) {
        ConstructBSTBySortedArray constructBSTBySortedArray = new ConstructBSTBySortedArray();
        System.out.println(constructBSTBySortedArray.sortedArrayToBST(new int[]{0, 1, 2}));
    }
}